//
//  ContractInfoGrabViewController.m
//  Jyxt
//
//  Created by 刘日 on 17/5/17.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "ContractInfoGrabViewController.h"
#import "ContractInfoGrabRootDelegate.h"
#import "ContractInfoGrabRootDataSource.h"
#import "FormCommitButtonTableViewCell.h"
#import "ContractInfoGrabViewModel.h"
#import "PhotosTableViewCell.h"
#import <LemonBubble.h>

@interface ContractInfoGrabViewController ()

@property (nonatomic , strong) ContractInfoGrabRootDelegate *rootDelegate;
@property (nonatomic , strong) ContractInfoGrabRootDataSource *rootDataSource;
@property (nonatomic , strong) NSArray *identifiers;
@property (nonatomic , strong) FormCommitButtonTableViewCell *submitButton;

@end

@implementation ContractInfoGrabViewController

- (void)initBaseInfo{
    self.title = @"合同信息采集";
}

- (NSArray *)identifiers{
    if (!_identifiers) {
        _identifiers = @[@"PhotosTableViewCell",@"FormInputTableViewCell"];
    }
    return _identifiers;
}

- (ContractInfoGrabRootDelegate *)rootDelegate{
    if (!_rootDelegate) {
        _rootDelegate = [ContractInfoGrabRootDelegate new];
    }
    return _rootDelegate;
}

- (ContractInfoGrabRootDataSource *)rootDataSource{
    if (!_rootDataSource) {
        _rootDataSource = [ContractInfoGrabRootDataSource new];
    }
    return _rootDataSource;
}

- (FormCommitButtonTableViewCell *)submitButton{
    if (!_submitButton) {
        _submitButton = [[[NSBundle mainBundle] loadNibNamed: @"FormCommitButtonTableViewCell" owner: self options: nil] lastObject];
        [[_submitButton.button rac_signalForControlEvents: UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UIAlertController *alert = [UIAlertController  alertControllerWithTitle: @"提交确认" message: @"您确认要提交信息吗？" preferredStyle: UIAlertControllerStyleAlert];
            [alert addAction: [UIAlertAction actionWithTitle: @"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                UITextField *noTextField = [[LKInstancePool defaultPool] objectForKey: @"contractNo"];
                UITextField *remarkTextField = [[LKInstancePool defaultPool] objectForKey: @"contractRemark"];
                [ContractInfoGrabViewModel applyContractInfoWithNo: noTextField.text remarks: remarkTextField.text photos: [PhotosTableViewCell photos] whenSuccess:^{
                    [self showRightWithTitle: @"发布成功" autoCloseTime: 2];
                    [[PhotosTableViewCell photos] removeAllObjects];
                    [[LKFreeCtrl getTopViewController].navigationController popViewControllerAnimated: YES];
                } failed:^(NSString *reason) {
                    [self showErrorWithTitle: reason autoCloseTime: 3];
                }];
            }]];
            [alert addAction: [UIAlertAction actionWithTitle: @"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }]];
            [self presentViewController: alert animated: YES completion: nil];
        }];
    }
    return _submitButton;
}

- (void)initView{
    self.rootTableView.delegate = self.rootDelegate;
    self.rootTableView.dataSource = self.rootDataSource;
    [[PhotosTableViewCell photos] removeAllObjects];
    for (NSString *name in self.identifiers) {
        [self.rootTableView registerNib: [UINib nibWithNibName: name bundle: [NSBundle mainBundle]] forCellReuseIdentifier: name];
    }
    self.rootTableView.tableFooterView = self.submitButton.contentView;
}

@end
